<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/25 0025
 * Time: 10:01
 */
//swoole 里面 后续所有 task异步 任务 都放在这里
namespace app\common\lib\task;
use app\common\lib\ali\Sms;
use app\common\lib\Redis;
use app\common\lib\redis\Predis;
class Task
{
    /**
     * 异步发送短信验证码
     * @param $data
     * @param $serv  swoole对象
     * @return string
     */
    public function sendSms($data,$serv)
    {
        try{
            //$response = $abj::sendSms($data['phone], $data['code']);
            $response =  [
                'RequestId' => '8906582E-6722',
                'Code' => 'OK',
                'Message' => '请求成功',
                'BizId' => '134523^4351232'
            ];
        }catch (\Exception $e){
            // todo
//            return Util::show(config('code.error'), '阿里大鱼内部异常');
            return $e->getMessage();
        }

        //如果发送成功，把验证码记录到redis里面(同步)
        if($response['Code'] === 'OK'){
            Predis::getInstance()->set(Redis::smsKey($data['phone']), $data['code'], config('redis.out_time'));
        }else{
            return false;
        }
        return true;  //告诉worker，调finish
    }

    /**
     * 通过task机制发送赛况实时数据给用户
     * @param $data
     * @param $serv  swoole对象
     */
    public function pushLive($data,$serv)
    {
        //获取连接的用户
        //赛况入库 //组装数据，push到直播页面
        $clients = Predis::getInstance()->sMembers(config('redis.live_game_key'));
        foreach($clients as $fd){
            $serv->push($fd, json_encode($data));
        }
        return true;  //告诉worker，调finish
    }


}










